<?php
/**
 * @user:andy
 * @date:2021/7/4
 * @company:http://www.sjdphp.com
 * @desc: 板块管理类
 */

namespace app\admin\controller;


use app\common\controller\BackBaseController;
use app\common\server\Json;
use app\common\server\Tree;

class Plate extends BackBaseController
{
    protected $model_id = 'plate_id';
    protected $model_name = 'plate_name';
    protected $model_desc = 'plate_sort desc';
    protected $model_status = 'plate_status';

    public function lists()
    {
        ## 收参
        $page = $this->request->param("page",0,'intval');
        $limit = $this->request->param("limit",0,'intval');
        $searchParams = json_decode($this->request->param("searchParams"),true);

        ## 默认查询数据的状态
        $term[] = [$this->model_status,'<>',config("project.mysql_code.table_delete")];

        ## 判断是否存在搜索数据
        if(!empty($searchParams)){
            if(isset($searchParams['name']) && $searchParams['name'] !=''){
                $term[] = [$this->model_name,'like','%'.$searchParams['name'].'%'];
            }
        }

        ## 获取实例
        $class = self::getBusExample($this->request->controller());
        $res = $class::getLists($term,'',$page,$limit,$this->model_desc);

        ## 判断请求方式并返回数据
        if($this->request->isPost()){
            return $res;
        }

        return view();
    }

    public function add(){
        if($this->request->isPost()){
            $param = $this->request->param();

            ## 校验参数
            $validate = self::getBusExample($this->request->controller(),2);
            if(!$validate->scene("add")->check($param)) return Json::error($validate->getError());

            ## 删除zyUpload
            if(isset($param["file"])){
                unset($param["file"]);
            }

            ## 默认状态
            $param[$this->model_status] = config("project.mysql_code.table_pending");

            ## 数据入库
            $class = self::getBusExample($this->request->controller());
            $res = $class::insertOrUpdateAndDel($param);

            return $res;
        }else{
            return view('',[
                'plates' => $this->getPlateLists(),
            ]);
        }
    }



    public function edit(){
        if($this->request->isPost()){
            $param = $this->request->param();

            ## 校验参数
            $validate = self::getBusExample($this->request->controller(),2);
            if(!$validate->scene("edit")->check($param)) return Json::error($validate->getError());

            ## 删除zyUpload
            if(isset($param["file"])){
                unset($param["file"]);
            }
            ## 数据入库
            $class = self::getBusExample($this->request->controller());
            $res = $class::insertOrUpdateAndDel($param,2);

            return $res;
        }else{
            $plate_id = $this->request->param("plate_id",0,'intval');
            if(!empty($plate_id)){
                $class = self::getBusExample($this->request->controller());
                $info = $class::getInfo(array($this->model_id => $plate_id));
            }
            return view('',[
                'info'   => $info,
                'plates' => $this->getPlateLists(),
            ]);
        }
    }


    /** 获取板块列表
     * @throws \ReflectionException
     */
    private function getPlateLists(){
        $class = self::getBusExample($this->request->controller());
        $lists = $class::getArrayLists(array([$this->model_status,'<>',config("project.mysql_code.table_delete")]),'plate_id,plate_name,pid',0,0,$this->model_desc);
        return Tree::getPlateTree($lists);
    }
}